package com.nostop.service;

import com.google.common.base.Preconditions;
import com.nostop.bean.PageQuery;
import com.nostop.bean.PageResult;
import com.nostop.common.RequestHolder;
import com.nostop.exception.ParamException;
import com.nostop.mapper.SysUserMapper;
import com.nostop.model.SysUser;
import com.nostop.param.UserParam;
import com.nostop.util.BeanValidator;
import com.nostop.util.IpUtil;
import com.nostop.util.MD5Util;
import com.nostop.util.PasswordUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    public void save(UserParam userParam) {
        BeanValidator.checkParam(userParam);
        if (checkTelephoneExist(userParam.getTelephone(),userParam.getId())) {
            throw new ParamException("电话已经被占用");
        }
        if (chechEmailExist(userParam.getMial(),userParam.getId())) {
            throw new ParamException("邮箱已经被占用");
        }

        String password = PasswordUtil.randomPassword();
        //TODO:sendEmial
        password = "123456";
        //加密存储
        String encryptedPassword = MD5Util.encrypt(password);
        SysUser sysUser = SysUser.builder().username(userParam.getUsername())
                .telephone(userParam.getTelephone())
                .mial(userParam.getMial())
                .deptId(userParam.getDeptId())
                .status(userParam.getStatus())
                .password(encryptedPassword)
                .remark(userParam.getRemark())
                .build();
        sysUser.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysUser.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysUser.setOperateTime(new Date());
        sysUserMapper.insertSelective(sysUser);
        //TODO:sendEmail

    }

    public void update(UserParam userParam) {
        BeanValidator.checkParam(userParam);
        if (checkTelephoneExist(userParam.getTelephone(),userParam.getId())) {
            throw new ParamException("电话已经被占用");
        }
        if (chechEmailExist(userParam.getMial(),userParam.getId())) {
            throw new ParamException("邮箱已经被占用");
        }
        SysUser before = sysUserMapper.selectByPrimaryKey(userParam.getId());
        Preconditions.checkNotNull(before,"待更新的用户不存在");
        SysUser after = SysUser.builder().id(userParam.getId()).username(userParam.getUsername())
                .telephone(userParam.getTelephone())
                .mial(userParam.getMial())
                .deptId(userParam.getDeptId())
                .status(userParam.getStatus())
                .password(before.getPassword())
                .remark(userParam.getRemark())
                .build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(after);
    }

    public boolean chechEmailExist(String mail,Integer userId) {
        return sysUserMapper.countByMail(mail,userId) > 0;
    }

    public boolean checkTelephoneExist(String phone,Integer userId) {
        return sysUserMapper.countByTelephone(phone,userId) > 0;
    }

    public SysUser findByKeyword(String keyword) {
        return sysUserMapper.findByKeyword(keyword);
    }

    public PageResult<SysUser> getPageByDeptId(int deptId, PageQuery page) {
            BeanValidator.checkParam(page);
            int count = 0;
            count = sysUserMapper.countBydeptId(deptId);
            if (count > 0) {
                List<SysUser> list = sysUserMapper.getPageByDeptId(deptId,page);
                //这里的强转也关键
                return PageResult.<SysUser>builder().total(count).data(list).build();
            }
            return PageResult.<SysUser> builder().build();
    }
}
